package cn.tedu.store.temperatureDetection.service.impl;

import cn.tedu.store.common.ApiResult;
import cn.tedu.store.temperatureDetection.entity.ComHospitalZhiyitongUser;
import cn.tedu.store.temperatureDetection.entity.dto.PatientInfoDto;
import cn.tedu.store.temperatureDetection.mapper.ComHospitalZhiyitongUserMapper;
import cn.tedu.store.temperatureDetection.mapper.CustomerInfoMapper;
import cn.tedu.store.temperatureDetection.mapper.FuYouCustomerInfoMapper;
import cn.tedu.store.temperatureDetection.mapper.FuYouPatientInfoMapper;
import cn.tedu.store.temperatureDetection.mapper.PatientInfoMapper;
import cn.tedu.store.temperatureDetection.mapper.PerBasinfregInfoMapper;
import cn.tedu.store.temperatureDetection.mapper.RMTJMapper;
import cn.tedu.store.temperatureDetection.mapper.ZhongYiPatientInfoMapper;
import cn.tedu.store.temperatureDetection.service.PatientInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * @author 75936
 * @version 1.0
 * @project demo_hesuan
 * @description
 * @date 2022-11-29 10:45:02
 */
@Service
public class PatientInfoServiceImpl implements PatientInfoService {
    @Autowired
    private PatientInfoMapper patientInfoMapper;
    @Autowired
    private FuYouPatientInfoMapper fuYouPatientInfoMapper;
    @Autowired
    private ZhongYiPatientInfoMapper zhongYiPatientInfoMapper;
    @Autowired
    private RMTJMapper rmtjMapper;
    @Autowired
    private FuYouCustomerInfoMapper fuYouCustomerInfoMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Autowired
    private PerBasinfregInfoMapper perBasinfregInfoMapper;

    @Autowired
    private ComHospitalZhiyitongUserMapper comHospitalZhiyitongUserMapper;
    @Override
    public List<PatientInfoDto> findPatientInfo(String idEnNo) {
        ArrayList<PatientInfoDto> list = new ArrayList<>();
        List<PatientInfoDto> peopleList =  patientInfoMapper.findPatientInfo(idEnNo);
        List<PatientInfoDto> peopleList1 =  patientInfoMapper.findFinIprInmaininfo(idEnNo);
        if (peopleList1!=null){
            peopleList.addAll(peopleList1);
        }
        List<PatientInfoDto> peopleList2 =  patientInfoMapper.findFinOprRegister(idEnNo);
        if (peopleList2!=null){
            peopleList.addAll(peopleList2);
        }
        if (peopleList!=null){
            peopleList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江人民医院");
            });
            list.addAll(peopleList);
        }

        List<PatientInfoDto> fuYouList= fuYouPatientInfoMapper.findPatientInfo(idEnNo);
        if (fuYouList!=null){
            fuYouList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江妇幼人民医院");
            });
            list.addAll(fuYouList);
        }
        List<PatientInfoDto> zhongYiList =zhongYiPatientInfoMapper.findPatientInfo(idEnNo);
        if (zhongYiList!=null){
            zhongYiList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江中医人民医院");
            });
            list.addAll(zhongYiList);
        }
        List<PatientInfoDto> fuYouCustomerInfoList = fuYouCustomerInfoMapper.findFuYouTiJian(idEnNo);
        if (fuYouCustomerInfoList!=null){
            fuYouCustomerInfoList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江妇幼人民医院");
            });
            list.addAll(fuYouCustomerInfoList);
        }
        List<PatientInfoDto> zhongYiCustomerInfoList =  customerInfoMapper.findPhone(idEnNo);
        if (zhongYiCustomerInfoList!=null){
            zhongYiCustomerInfoList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江中医人民医院");
            });
            list.addAll(zhongYiCustomerInfoList);
        }
        List<PatientInfoDto> perBasinfregInfoList =  perBasinfregInfoMapper.findPerBasinfregInfo(idEnNo);
        if (perBasinfregInfoList!=null){
            perBasinfregInfoList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江公共卫生系统");
            });
            list.addAll(perBasinfregInfoList);
        }
        List<PatientInfoDto> rmtjList = rmtjMapper.findPhone(idEnNo);
        if (rmtjList!=null){
            rmtjList.stream().forEach(patientInfoDto -> {
                patientInfoDto.setHospitalName("枝江公共卫生系统");
            });
            list.addAll(rmtjList);
        }
        List<PatientInfoDto> collect=list.stream().filter(s->
                s.getHomeTel()!=null).collect(Collectors.toList());
        //List<PatientInfoDto> list1 =collect.stream().collect(Collectors.collectingAndThen
        //        (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing
        //                (PatientInfoDto::getIdEnNo ))), ArrayList::new));
        List<PatientInfoDto> list3 = collect.stream().collect(Collectors.collectingAndThen
                (Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing
                        (PatientInfoDto::getHospitalName ))), ArrayList::new));
        return list3;
    }

    @Override
    public ApiResult findByUserName(String userName,String passWord) {
        ComHospitalZhiyitongUser user = comHospitalZhiyitongUserMapper.findByUserName(userName);
        if (!user.getPassWord().equals(passWord)&&user.getPassWord()!=null&&!user.getPassWord().equals("")){
            return ApiResult.fail("密码输入有误");
        }else {
            return ApiResult.ok(user);
        }


    }

    @Override
    public List<PatientInfoDto> findPatientInfoByPhone(String phone) {
        return null;
    }
}
